package com.t.bricks.bussiness.service.bridge.impl;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingClass;
import org.springframework.stereotype.Service;

import com.t.bricks.bussiness.dao.bridge.BridgeDao;
import com.t.bricks.bussiness.service.bridge.IgnoreURLService;
import com.t.bricks.model.MsgEmity;

/**
 * 拦截器忽略路径接口桥接实现
 * @author huangxinbian
 */
@ConditionalOnMissingClass("com.t.bricks.bussiness.service.ignoreurl.impl.IgnoreURLServiceImpl")
@Service
public class IgnoreURLServiceImpl implements IgnoreURLService {

	@Autowired
	private BridgeDao bridgeDao;

	/**
	 * 桥接到登录服务的服务名,默认TBaseSystemServer
	 */
	@Value("${CloudServerBaseServer:TBaseSystemServer}")
	private String ignoreURLServerName;
	
	/**
	 * 根据记录编号取对象
	 * @param sId 记录编号
	 * @return
	 */
	public MsgEmity findById(String sId) {
		Map<String, Object> map = new HashMap<>(1);
		map.put("sId", sId);
		
		MsgEmity me = bridgeDao.post(ignoreURLServerName, "/ignore/url/find/id",
				map, MsgEmity.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}
	
	/**
	 * 验证指定url是否在可忽略的访问路径中(给内部拦截器用,直接返回Boolean)
	 * @param sUrl 待检验的url
	 * @param isMustLogin 是否必须登录
	 * @return
	 */
	public boolean checkIgnoreUrl(String sUrl, Integer iMustLogin, String sUserType) {
		Map<String, Object> map = new HashMap<>(2);
		map.put("sUrl", sUrl);
		map.put("iMustLogin", iMustLogin);
		map.put("sUserType", sUserType);
		
		MsgEmity me = bridgeDao.post(ignoreURLServerName, "/ignore/url/check",
				map, boolean.class, false);
		if (!me.isSuccess()) {
			return false;
		}
		
		return me.getData();
	}
	
	/**
	 * 清理缓存
	 * @return
	 */
	public MsgEmity clearCache() {
		MsgEmity me = bridgeDao.post(ignoreURLServerName, "/ignore/url/clear/cache",
				null, boolean.class, false);
		if (!me.isSuccess()) {
			return me;
		}
		
		return me.getData();
	}
	
}
